<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2024/12/15
 * 描述:
 */

namespace app\middleware;


use app\result\AccessLogResult;

class Check
{
    public function handle($request, \Closure $next)
    {
        $header = $request->header();
        $versioncode = $header['versioncode'] ?? 0;//112
        $version = strtolower($header['version'] ?? '');//1.0.12
        $platform = strtolower($header['platform'] ?? '');//ios android
        $bs_from = strtolower($header['bsfrom'] ?? '');//h5/xcx/app
        $isOptions = $request->isOptions();
        //bs_log($header, 'header','header.log');
        !defined('BS_VERSIONCODE') && define('BS_VERSIONCODE', $versioncode);
        !defined('BS_VERSION') && define('BS_VERSION', $version);
        !defined('BS_OS') && define('BS_OS', $platform);
        !defined('BS_FROM') && define('BS_FROM', $bs_from);
        if (empty($isOptions)) {
            $url = $request->url();
            $ip = get_ip();
            $ipStatus = !in_array($ip, ['127.0.0.1', '0.0.0.0']);
            //$ipStatus = true;
            if (stripos($url, '/admin/') === false && $ipStatus) {
                $header['url'] = $url;
                $header['method'] = strtolower($request->method());
                $header['ip'] = get_ip();
                $header['time'] = date('Y-m-d H:i:s');
                //bs_log(['version' => $version, 'platform' => $platform], 'middleware', 'version.log');
                bs_queue(AccessLogResult::class, 'add', $header, time(), 5);
            }
        }
        return $next($request);
    }
}
